package com.bbx.taxi.client.Adapter.Chat;

import java.util.ArrayList;
import java.util.List;

import com.bbx.taxi.client.Bean.Line;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Bean.City;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX.ToCitiesBean;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean.ChildCitysBeanX.ToCitiesBean.ChildCitysBean;
import com.bbx.taxi.client.Bean.Line.CitiesBean.AppCitysBean;
import com.bbx.taxi.client.Util.Cn2Spell;

public class SearchCityAdapter extends BaseAdapter implements Filterable {
	private List<?> datas;
	private LayoutInflater mInflater;
	private Context mContext;
	private boolean isStart;
	List<ChildCitysBeanX> mResultCities_start ;
	List<ChildCitysBean> mResultCities_end ;

	// private String mFilterStr;

	public SearchCityAdapter(Context context,List<?> datas,boolean isStart) {
		mContext = context;
		this.datas = datas;
		this.isStart=isStart;
		mResultCities_start = new ArrayList<ChildCitysBeanX>();
		mResultCities_end = new ArrayList<ChildCitysBean>();
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		if(isStart){
			return mResultCities_start==null?0:mResultCities_start.size();
		}else{
			return mResultCities_end==null?0:mResultCities_end.size();
		}
	}

	@Override
	public Object getItem(int position) {
		if(isStart){
			return mResultCities_start.get(position);
		}else{
			return mResultCities_end.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	public String getCity(int position){
		if(isStart){
			return mResultCities_start.get(position).getName();
		}else{
			return mResultCities_end.get(position).getName();
		}
	}
	public String getDetail(int position){
		if(isStart){
			return mResultCities_start.get(position).getIs_detail();
		}else{
			return mResultCities_end.get(position).getIs_detail();
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listitem_search_city, null);
		}
		TextView provinceTv = (TextView) convertView
				.findViewById(R.id.search_province);
//		if(getProvince(position)!=null&&!getProvince(position).equals("")){
//			provinceTv.setVisibility(View.VISIBLE);
//			provinceTv.setText(getProvince(position));
//		}else{
//			provinceTv.setVisibility(View.GONE);
//		}
		provinceTv.setVisibility(View.GONE);

		TextView cityTv = (TextView) convertView
				.findViewById(R.id.column_title);
		cityTv.setText(getCity(position));
		return convertView;
	}

	@Override
	public Filter getFilter() {
		Filter filter = new Filter() {
			protected void publishResults(CharSequence constraint,
					FilterResults results) {
				if(isStart){
					mResultCities_start = (ArrayList<ChildCitysBeanX>) results.values;
				}else{
					mResultCities_end = (ArrayList<ChildCitysBean>) results.values;
				}
				if (results.count > 0) {
					notifyDataSetChanged();
				} else {
					notifyDataSetInvalidated();
				}
			}

			protected FilterResults performFiltering(CharSequence s) {
				//强制转化大写
				String str = s.toString().toUpperCase();
				// mFilterStr = str;
				FilterResults results = new FilterResults();
				List<ChildCitysBeanX> list_startchidcity = new ArrayList<>();
				List<ChildCitysBean> list_endchidcity = new ArrayList<>();
				if (datas!=null&&datas.size()>0) {
					if(isStart){
						for(AppCitysBean mAppCitysBean:(List<AppCitysBean>) datas){
							for(ChildCitysBeanX mChildCitysBeanX:mAppCitysBean.getChild_citys()){
								String name=mChildCitysBeanX.getName();
								Cn2Spell mCn2Spell=Cn2Spell.getInstance();
								// 匹配全屏、首字母、和城市名中文
								String allpy=mCn2Spell.converterToSpell(name).toUpperCase();////强制转化大写
								String AllFristPY= mCn2Spell.onCn2Spell(name).toUpperCase();////强制转化大写
								String firstPY=AllFristPY.length()>=1?AllFristPY.charAt(0)+"":"";
								if (allpy.indexOf(str) > -1
										|| AllFristPY.indexOf(str) > -1
										|| name.indexOf(str) > -1
										|| firstPY.indexOf(str) > -1) {
									list_startchidcity.add(mChildCitysBeanX);
								}
							}
						}
					}else{
						for(ToCitiesBean mToCitiesBean:(List<ToCitiesBean>) datas){
                            for(ChildCitysBean mChildCitysBean:mToCitiesBean.getChild_citys()){
								String name=mChildCitysBean.getName();
								Cn2Spell mCn2Spell=Cn2Spell.getInstance();
								// 匹配全屏、首字母、和城市名中文
								String allpy=mCn2Spell.converterToSpell(name).toUpperCase();////强制转化大写;
								String AllFristPY= mCn2Spell.onCn2Spell(name).toUpperCase();////强制转化大写;
								String firstPY=AllFristPY.length()>=1?AllFristPY.charAt(0)+"":"";
								if (allpy.indexOf(str) > -1
										|| AllFristPY.indexOf(str) > -1
										|| name.indexOf(str) > -1
										|| firstPY.indexOf(str) > -1) {
									list_endchidcity.add(mChildCitysBean);
								}
							}
						}
					}
				}
				results.values = isStart?list_startchidcity:list_endchidcity;
				results.count = isStart?list_startchidcity.size():list_endchidcity.size();
				return results;
			}
		};
		return filter;
	}

}
